package com.jsj.controller;

import com.jsj.service.ProblemService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.print.Doc;
import java.util.List;

@RestController
@RequestMapping("PreV")
public class PreProblem {

    private ProblemService problemService;

    @Autowired
    public void setProblemService(ProblemService problemService) {
        this.problemService = problemService;
    }

    @RequestMapping(value = "/ProblemId/{Id}", method = {RequestMethod.GET})
    public String Prevproblems(@PathVariable("Id") int topicId){

        List<String> titles=problemService.queryTitleByTopicId(topicId);

        String str="<div class=\"wrap1000 clearfix con\">"+
                "<div class=\"width1100_smile\">"+
                "<div class=\"RightCon\" id=\"RightCon\">"+
                "<div class=\"radiusBG\">"+
                "<div class=\"CeYan\">"+
                "<div class=\"ZyBottom\"  id=\"ZyBottom\"></div></div></div></div></div></div>";
        Document document= Jsoup.parse(str);
        Element Relement=document.getElementById("ZyBottom");
        int a=1;
        for(String title:titles){
            Document docm=Jsoup.parse(title);
            Element element=docm.select("p").first();
            String p=element.text();
            element=docm.select("img[src]").first();
            String imgSrc=element.attr("src");
            Relement.append("<div class=\"TiMu\"><div class=\"Zy_TItle clearfix\"><i class=\"fl\">"+a+"</i><div class=\"Zy_TItle_p\"><p>"+p+"</p><cc><p>"+"<img src=\""+imgSrc+"\"  width=\"500px\"></p></cc></div></div></div>");

        }
        Element element1=document.getElementsByTag("head").first();
        element1.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://mooc1.zut.edu.cn/css/statistic/common/moocLeft.css\">");
        element1.append("<link href=\"http://mooc1.zut.edu.cn/space/new/moocIndex/css/ZuoYe.css?v=2020-1028-2041\" type=\"text/css\" rel=\"stylesheet\">");
        element1.append("<style>\n" +
                "\t.Zy_TItle_p{width:934px;}\n" +
                "\t.Zy_TItle p{display:block;}\n" +
                "    em{font-style: italic;}\n" +
                "    .Cy_TItle1 h2{ font:bold 16px/40px \"Microsoft YaHei\"; color:#202020;margin-top:20px;}\n" +
                "</style>");
        str=document.html();
        return str;
    }
}
